$(function () {
    obj.init();

});

var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        init:function () {
            this.dataGrid = new xhDataGrid($('#list'), 'lm/receive/getReturn');
            this.dataGrid.parseToolbar().init().activeButton([], ['bt_list_del','bt_list_confirm']);
            this.funObj().bindDataGridEvent();
            this.listener();
        },
        listener:function () {
            var $this = this;

            //新增事件按钮
            $('#bt_list_add').on('click',function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().openAddForm();
            });

            $('#bt_list_del').on('click',function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().askDelQuestion();
            });

            $('#bt_list_search').click(function () {
                $this.funObj().search();
            });

            $('#bt_list_confirm').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().showGetQR();
            });

        },
        funObj:function () {
            var $this = this;
            return {
                bindDataGridEvent:function () {
                    $this.dataGrid.command({
                        view:detailview,
                        detailFormatter:function(index,row){
                            var content = '';
                            var str='<div class="ddv" style="padding:5px 0;overflow: auto;"  ><table style="width: 100%;height: 100%"  >';
                            if (row['mProdList'].length !== 0){
                                $.each(row['mProdList'], function(index, row){
                                    content += '<tr><td style="text-align: left">' + row.name + '</td><td style="text-align: right">' + row.num + '</td></tr>';
                                });
                                str+='<tr><th style="text-align: center" width="75%">名称</th><th style="text-align: center" width="25%">数量</th></tr>' + content;
                            }
                            str+='</table></div>';
                            return str;
                        },
                        onExpandRow: function(index,row){
                            var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                            ddv.panel({
                                // height:100,
                                width:300,
                                border:true,
                                cache:false,
                                onLoad:function(){
                                    $this.dataGrid.command('fixDetailRowHeight',index);
                                    $this.dataGrid.command('fixDetailRowWidth',index);
                                }
                            });
                            $this.dataGrid.command('fixDetailRowHeight',index);
                        }
                    });
                },
                openAddForm:function () {
                    window.top.xhTop.showModal({
                        title: '发起归还',
                        content: '<iframe  src="'+baseUrl+'lm/receive/returnForm"  style="width: 100%;;border-style: none;padding: 0px;height:400px"  scrolling="yes"   ></iframe>',
                        button:'提交',
                        width:800,
                        backClose:false
                    });
                },
                askDelQuestion:function () {
                    var iLRTId = $this.dataGrid.getSelectValues('iLRTId');
                    xhExtra.askChangeStatus('lm/receive/delReturn',{iLRTId:iLRTId},null,null,function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        $this.dataGrid.reload();
                    });
                },
                search:function () {
                    var key        = xhExtra.getValue($('#list_search'));
                    var state      = xhExtra.getValue($('#select_state'));
                    var consume      = xhExtra.getValue($('#select_consume'));
                    $this.dataGrid.reload({key : key , state : state , consume : consume});
                },
                showGetQR:function () {
                    var iState = $this.dataGrid.getSelectValue('iState');
                    if (iState === '已归还') {
                        window.top.xhTop.showError('该申请已经确认归还，不可重复确认');
                        return false;
                    }
                    xhExtra.ajaxRequest('lm/receive/getReturnQR', {number: $this.dataGrid.getSelectValue('sNumber')}, {check: true}, function(result) {
                        window.top.swal({
                            title: '归还清单-' + $this.dataGrid.getSelectValue('sUserName'),
                            html: '<img src="' + result.msg + '" width="50%">',
                            showConfirmButton: true,
                            confirmButtonText: '确认'
                        }).then(function () {}, function (dismiss) {});
                    });

                }
            }
        }
    }
};